package com.xydtech.ecard.merchant.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.merchant.domain.EcardPersonArea;
import com.xydtech.ecard.merchant.service.IEcardPersonAreaService;
import com.xydtech.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片个人网格信息Controller
 *
 * @author yuzhiguo
 * @date 2024-09-14 上午10:20
 */
@Controller
@RequestMapping("/merchant/personArea")
public class EcardPersonAreaController extends BaseController
{
    private String prefix = "merchant";

    @Autowired
    private IEcardPersonAreaService ecardPersonAreaService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("merchant:personArea:view")
    @GetMapping()
    public String person()
    {
        return prefix + "/personArea";
    }

    /**
     * 查询电子名片个人网格区域信息列表
     */
    @RequiresPermissions("merchant:personArea:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardPersonArea ecardPersonArea)
    {
        String salt = getSalt();//盐值
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardPersonArea.setDictDeptId(sysDept.getDeptId());
        startPage();
        List<EcardPersonArea> list = ecardPersonAreaService.selectEcardPersonAreaList(ecardPersonArea, salt);
        return getDataTable(list);
    }

    /**
     * 导出电子名片个人网格区域信息列表
     */
    @RequiresPermissions("merchant:personArea:export")
    @Log(title = "电子名片个人网格区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardPersonArea ecardPersonArea)
    {
        String salt = getSalt();//盐值
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardPersonArea.setDictDeptId(sysDept.getDeptId());
        List<EcardPersonArea> list = ecardPersonAreaService.selectEcardPersonAreaList(ecardPersonArea, salt);
        ExcelUtil<EcardPersonArea> util = new ExcelUtil<EcardPersonArea>(EcardPersonArea.class);
        return util.exportExcel(list, "电子名片个人网格区域信息数据");
    }

    /**
     * 选择转交的客户经理
     */
    @RequiresPermissions("merchant:personArea:handOver")
    @GetMapping("/handOver/{ids}")
    public String choiceUserByHand(@PathVariable("ids") String ids, ModelMap mmap) {
        mmap.put("ids", ids);
        mmap.put("merchantType", "6");//商户类型：1-个人，2-商户，3-企业，4-新型农业主体，5-企业场所，6-个人网格
        return prefix + "/choiceUser";
    }

    /**
     * 网格转交
     */
    @Log(title = "网格转交客户经理", businessType = BusinessType.UPDATE)
    @PostMapping("/allocation")
    @ResponseBody
    public AjaxResult allocation(Long registId,Long deptId, Long[] ids, Long userId) {
        /**查询机构*/
        //SysUser user = sysUserService.selectUserById(userId);
        if(registId == null){
            return AjaxResult.error("请选择移交人员！");
        }
        if(deptId == null){
            return AjaxResult.error("请选择移交的机构！");
        }
        //移交网格列表
        List<EcardPersonArea> ecardPersonAreaList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            //修改创建人id和创建人机构id
            EcardPersonArea ecardPersonArea = new EcardPersonArea();
            ecardPersonArea.setId(ids[i]);
            ecardPersonArea.setManagementPersonnelId(registId);
            ecardPersonArea.setDeptId(deptId);
            ecardPersonAreaList.add(ecardPersonArea);
        }
        return toAjax(ecardPersonAreaService.allocationEcardPersonAreaInfo(ecardPersonAreaList,registId, userId));
    }

   /**
    * 启用/停用网格
    *
    * @author yuzhiguo
    * @date 2024/9/14 上午11:39
    */
    @RequiresPermissions("merchant:personArea:switchOver")
    @Log(title = "启用/停用网格", businessType = BusinessType.UPDATE)
    @PostMapping("/switchOver")
    @ResponseBody
    public AjaxResult switchOver(Long[] ids, Integer state) {
        //网格列表
        List<EcardPersonArea> ecardPersonAreaList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            //修改创建人id和创建人机构id
            EcardPersonArea ecardPersonArea = new EcardPersonArea();
            ecardPersonArea.setId(ids[i]);
            ecardPersonArea.setState(state);
            ecardPersonAreaList.add(ecardPersonArea);
        }
        int i = ecardPersonAreaService.batchSwitchOverEcardPersonAreaInfo(ecardPersonAreaList);
        return toAjax(i);
    }
}
